package com.codeman.web.controller.book;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeman.business.book.mapper.BookMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.codeman.common.annotation.Log;
import com.codeman.common.core.controller.BaseController;
import com.codeman.common.core.domain.AjaxResult;
import com.codeman.common.enums.BusinessType;
import com.codeman.business.book.domain.Book;
import com.codeman.business.book.service.IBookService;
import com.codeman.common.utils.poi.ExcelUtil;
import com.codeman.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 小说Controller
 * 
 * @author codeman
 * @date 2021-03-19
 */
@RestController
@RequestMapping("/book/book")
public class BookController extends BaseController
{
    @Autowired
    private IBookService bookService;

    /**
     * 查询小说列表
     */
    @PreAuthorize("@ss.hasPermi('book:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                              @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,Book book)
    {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>(book);
        Page<Book> page = new Page<>(pageNo, pageSize);
        IPage<Book> list = bookService.page(page,queryWrapper);
        return getDataTable(list.getRecords());
    }

    /**
     * 导出小说列表
     */
    @PreAuthorize("@ss.hasPermi('book:book:export')")
    @Log(title = "小说", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(@RequestParam Map query)
    {
        List<Book> list = bookService.listByMap(query);
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        return util.exportExcel(list, "book");
    }

    /**
     * 获取小说详细信息
     */
    @PreAuthorize("@ss.hasPermi('book:book:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bookService.getById(id));
    }

    /**
     * 新增小说
     */
    @PreAuthorize("@ss.hasPermi('book:book:add')")
    @Log(title = "小说", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Book book)
    {
        return toAjax(bookService.save(book));
    }

    /**
     * 修改小说
     */
    @PreAuthorize("@ss.hasPermi('book:book:edit')")
    @Log(title = "小说", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Book book)
    {
        return toAjax(bookService.saveOrUpdate(book));
    }

    /**
     * 删除小说
     */
    @PreAuthorize("@ss.hasPermi('book:book:remove')")
    @Log(title = "小说", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
       return toAjax(bookService.removeByIds(Arrays.asList(ids)));
    }
}
